package cn.gogpay.dcb.api.web.base;

import cn.gogpay.dcb.common.dto.ResultCode;
import cn.gogpay.dcb.common.dto.ResultDTO;
import cn.gogpay.dcb.h5base.common.util.SessionUtils;
import cn.gogpay.dcb.h5base.dto.UserDTO;
import cn.gogpay.dcb.springboot.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import javax.validation.Validator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description
 *
 * @author huangting@gogpay.cn
 * @date 2019/10/14 9:24
 */
public class BaseController {
    /**
     * 验证Bean实例对象
     */
    @Autowired
    private Validator validator;

    @Value("${spring.profiles.active}")
    private String dev;

    /**
     * 服务端参数有效性验证
     *
     * @param object 验证的实体对象
     * @param groups 验证组
     */
    protected void beanValidator(Object object, Class<?>... groups) throws ValidationException {
        try {
            BeanValidator.validateWithException(validator, object, groups);
        } catch (ConstraintViolationException ex) {
            List<String> list = BeanValidator.extractPropertyAndMessageAsList(ex, ": ");
            String msg = list == null || list.size() == 0 ? "数据校验失败" : list.get(0);
            if(msg.contains(":")){
                String[] arr = msg.split(":");
                if(arr.length >1){
                    msg = arr[1];
                }
            }
            throw new ValidationException(msg);
        }
    }

    /**
     * 返回成功
     *
     * @param msg
     * @return
     */
    protected ResultDTO getSuccessResult(String msg) {
        ResultDTO resultDTO = new ResultDTO(ResultCode.SUCCESS_CODE, msg);
        return resultDTO;
    }

    /**
     * 返回成功与数据
     *
     * @param msg
     * @param data
     * @return
     */
    protected ResultDTO getSuccessResult(String msg, Object data) {
        ResultDTO resultDTO = new ResultDTO(ResultCode.SUCCESS_CODE, msg, data);
        return resultDTO;
    }

    /**
     * 返回只有一个key的map的result
     *
     * @param msg 返回信息
     * @param key key
     * @param object data
     * @return resultDto
     */
    protected ResultDTO getSuccessResult(String msg, String key, Object object) {
        Map<String, Object> map = new HashMap<>(4);
        map.put(key, object);
        ResultDTO successResult = this.getSuccessResult(msg, map);
        return successResult;
    }

    /**
     * 获得用户id
     * @param request
     * @return
     */
    protected String getUserId(HttpServletRequest request) {
        if ("dev".equals(dev)) {
            return "1xxxxxxx3333ccccc";
        }
        return SessionUtils.getUserId(request);
    }

    /**
     * 获得用户信息
     * @param request
     * @return
     */
    protected UserDTO getUserInfo(HttpServletRequest request) {
        if ("dev".equals(dev)) {
            UserDTO userDTO = new UserDTO();
//            userDTO.setAuthName("焦明");
//            userDTO.setUserId("8aaa83ff6e82b3df016e86f4caf40086");
//            userDTO.setPhoneNo("15285574940");
//            userDTO.setAuthCardNo("522124198709176019");
            userDTO.setAuthName("焦明1");
            userDTO.setUserId("8aaa83ff6e82b3df016e86f4caf40086");
            userDTO.setPhoneNo("15285574940");
            userDTO.setAuthCardNo("522124198709176011");
            return userDTO;
        }
        return SessionUtils.getUserInfo(request);
    }


}
